package demo;

import java.io.*;

public class SerializableDemo {
    public static void main(String[] args) throws Exception {
        Foo foo = new Foo("fff", new Bar("hhh"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new ObjectOutputStream(baos).writeObject(foo);
        Object o = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
        System.out.println(o);
    }
}

class Foo implements java.io.Serializable {
    private String name;
    private Bar bar;

    public Foo(String name, Bar bar) {
        this.name = name;
        this.bar = bar;
    }

    private void readObject(ObjectInputStream in) {
        try {
            in.defaultReadObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

class Bar implements Serializable {
    private String f1;

    public Bar(String f1) {
        this.f1 = f1;
    }

    private void readObject(ObjectInputStream in) {
        try {
            in.defaultReadObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
